package com.t1.demo.RobotTest;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @Description:获取鼠标位置
 * @Author: 杨淮尊
 * @Date: 2020/4/13
 **/
public class GetMousePointer {
    public static void main(String[] args) throws IOException {
        //设置图片储存位置
        final String USER_HOME = System.getProperty("user.home");
        //获取本地图像设备
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();
        //使用图片代替光标
        File file = new File("C:\\Users\\2\\Desktop\\Test\\mouse.png");
        BufferedImage blackSquare = ImageIO.read(file);
        Image scaledInstance = blackSquare.getScaledInstance(15, 15, Image.SCALE_SMOOTH);

        //使用像素代替光标
        //BufferedImage blackSquare = new BufferedImage(50, 50, BufferedImage.TYPE_3BYTE_BGR);
//        for(int i = 0; i < blackSquare.getHeight(); i++){
//            for(int j = 0; j < blackSquare.getWidth(); j++){
//                blackSquare.setRGB(j, i, 300);
//            }
//        }

        try {
            Robot robot = new Robot();
            BufferedImage screenshot = robot.createScreenCapture(new Rectangle(0,0,width,height));
            PointerInfo pointer = MouseInfo.getPointerInfo();
            int x = (int) pointer.getLocation().getX();
            int y = (int) pointer.getLocation().getY();

            screenshot.getGraphics().drawImage(scaledInstance, x, y, null);
            ImageIO.write(screenshot, "PNG", new File(USER_HOME, "screenshot.PNG"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
